<template>
  <el-dialog title="编辑角色" :visible.sync="visible">
    <el-container>
      <el-main>
        <el-form :model="editForm" ref="editForm" :rules="rules" size="mini" label-width="100px" :label-position="labelPosition">
          <el-form-item label="角色名称" prop="rolename">
            <el-input type="text" v-model="editForm.rolename" clearable placeholder="角色名称"/>
          </el-form-item>
          <el-form-item label="角色说明" prop="rolenote">
            <el-input type="textarea" v-model="editForm.rolenote" clearable placeholder="角色说明"/>
          </el-form-item>
        </el-form>
      </el-main>
      <el-footer>
        <el-button :loading="editBtn" type="primary" @click="submitForm('editForm')">确定</el-button>
        <el-button @click="closeForm()">取消</el-button>
      </el-footer>
    </el-container>
  </el-dialog>
</template>

<script>
  import { editRoles } from '../../../api/system/functionRoles'

  export default {
    name: 'functionRolesEdit',
    props: ['editVisible', 'editForms'],
    watch: {
      editVisible: function(val) {
        this.visible = val
      },
      visible: function(val) {
        this.$emit('update:editVisible', val)
      },
      editForms: function(val) {
        this.editForm = val
      }
    },
    data() {
      return {
        visible: false,
        editBtn: false,
        labelPosition: 'right',
        editForm: {
          rolename: '',
          rolenote: ''
        },
        rules: {
          rolename: [
            { required: true, message: '请输入角色名称', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      submitForm: function(editForm) {
        this.$refs[editForm].validate((valid) => {
          if (valid) {
            this.editBtn = true
            editRoles(this.editForm).then(res => {
              this.editBtn = false
              this.visible = false
              this.$message({
                message: '修改成功',
                type: 'success'
              })
            })
          } else {
            return false
          }
        })
      },
      closeForm: function() {
        this.visible = false
      }
    }
  }
</script>

<style scoped>
.el-footer{
  text-align: center;
}
</style>
